package com.futures.investm.common;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.futures.investm.R;
import com.futures.investm.constant.BussConstant;
import com.futures.investm.utils.IntentUtils;
import com.aries.ui.view.title.TitleBarView;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVOSCloud;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.LogInCallback;
import com.avos.avoscloud.RequestMobileCodeCallback;
import com.avos.avoscloud.UpdatePasswordCallback;

import butterknife.BindView;
import butterknife.OnClick;
import tech.com.commoncore.base.BaseTitleActivity;
import tech.com.commoncore.utils.DataUtils;
import tech.com.commoncore.utils.RegUtils;
import tech.com.commoncore.utils.ToastUtil;
import tech.com.commoncore.widget.CountDownTextView;

public class RegistActivity extends BaseTitleActivity {


    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.tv_get_code)
    CountDownTextView tvGetCode;
    @BindView(R.id.btn_register)
    Button btnRegister;
    @BindView(R.id.checkbox)
    CheckBox checkbox;
    @BindView(R.id.txt)
    TextView txt;
    @BindView(R.id.tv_login)
    TextView tv_login;

    @Override
    public void setTitleBar(TitleBarView titleBar) {
        titleBar.setTitleMainText("注册")
                .setOnLeftTextClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        onBackPressed();
                    }
                })
                .setStatusBarLightMode(false);
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_regist;
    }

    @Override
    public void initView(Bundle savedInstanceState) {

    }

    @OnClick({R.id.tv_get_code, R.id.btn_register, R.id.txt,R.id.tv_login})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_get_code:
                //获取验证码
                getCode(etPhone.getText().toString().trim());
                break;


            case R.id.btn_register:
                //注册
                register(etPhone.getText().toString().trim(), etPassword.getText().toString().trim(), etCode.getText().toString().trim());
                break;


            //进入协议
            case R.id.txt:
                //协议
                IntentUtils.goProtocol(mContext);
                break;

            case R.id.tv_login:
                IntentUtils.goLoginActivity(mContext);
                this.finish();
                break;
        }
    }

    private void register(final String phone, final String password, String code) {
        //手机号码空校验
        if (DataUtils.isEmptyString(phone)) {
            ToastUtil.show("请输入手机号");
            return;
        }
        //手机号有效性校验
        if (!RegUtils.isMobile(phone)) {
            ToastUtil.show("请输入有效手机号");
            return;
        }

        //密码空校验
        if (DataUtils.isEmptyString(password)) {
            ToastUtil.show("请输入密码");
            return;
        }

        if (password.length() < 6) {
            ToastUtil.show("密码不能少于6位");
            return;
        }

        //验证码空校验
        if (DataUtils.isEmptyString(code)) {
            ToastUtil.show("请输入验证码");
            return;
        }

        if (!checkbox.isChecked()) {
            ToastUtil.show("注册账户前需阅读并同意《用户注册协议》");
            return;
        }

        AVUser.signUpOrLoginByMobilePhoneInBackground(phone, code, new LogInCallback<AVUser>() {
            @Override
            public void done(AVUser avUser, AVException e) {
                // 如果 e 为空就可以表示登录成功了，并且 user 是一个全新的用户
                if (e == null) {
                    // 注册成功，把用户对象赋值给当前用户 AVUser.getCurrentUser()
                    ToastUtil.show("注册成功");
                    updateNickName(avUser, phone);
                    setPw(avUser, password);
                    RegistActivity.this.finish();
                } else {
                    ToastUtil.show(e.getMessage());
                }
            }
        });
    }

    private void setPw(AVUser user, String password) {
        user.updatePasswordInBackground(user.get("password") + "", password, new UpdatePasswordCallback() {
            @Override
            public void done(AVException e) {

            }
        });
    }

    private void updateNickName(AVUser user, String phone) {
        //设置默认昵称
        user.put(BussConstant.NICKNAME, "用户" + phone.substring(7, 11));
        user.saveInBackground();
    }

    //获取验证码
    private void getCode(String phone) {
        //手机号码空校验
        if (DataUtils.isEmptyString(phone)) {
            ToastUtil.show("请输入手机号");
            return;
        }
        //手机号有效性校验
        if (!RegUtils.isMobile(phone)) {
            ToastUtil.show("请输入有效手机号");
            return;
        }
        AVOSCloud.requestSMSCodeInBackground(phone, new RequestMobileCodeCallback() {
            @Override
            public void done(AVException e) {
                if (null == e) {
                    /* 请求成功 */
                    tvGetCode.start();
                    ToastUtil.show("获取验证码成功");
                } else {
                    /* 请求失败 */
                    ToastUtil.show(e.getMessage());
                }
            }
        });

    }
}
